﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sicurezza.Entita;
using System.Xml.Serialization;
using System.IO;

namespace Sicurezza.Manager
{
    public class XmlManager
    {
        public static string Serialize(List<Pair> list)
        {
            XmlSerializer x = new XmlSerializer(list.GetType());
            string serializedList = "";
            using(MemoryStream ms = new MemoryStream())
            {
                x.Serialize(ms,list);
                serializedList = Encoding.ASCII.GetString(ms.ToArray());
            }

            return serializedList;
        }

        public static List<Pair> DeserializeList(string list)
        {
            List<Pair> deserializedList = new List<Pair>();
            XmlSerializer x = new XmlSerializer(deserializedList.GetType());
            byte[] byteArray = Encoding.ASCII.GetBytes( list );
            using (MemoryStream stream = new MemoryStream(byteArray))
            {
                deserializedList = x.Deserialize(stream) as List<Pair>;
            }

            return deserializedList;
        }

        public static string Serialize(MailMessage mm)
        {
            XmlSerializer x = new XmlSerializer(mm.GetType());
            string serializedMessage = "";
            using (MemoryStream ms = new MemoryStream())
            {
                x.Serialize(ms, mm);
                serializedMessage = Encoding.ASCII.GetString(ms.ToArray());
            }

            return serializedMessage;
        }

        public static MailMessage DeserializeMailMessage(string message)
        {
            MailMessage deserializedMessage = new MailMessage();
            XmlSerializer x = new XmlSerializer(deserializedMessage.GetType());
            byte[] byteArray = Encoding.ASCII.GetBytes(message);
            using (MemoryStream stream = new MemoryStream(byteArray))
            {
                deserializedMessage = x.Deserialize(stream) as MailMessage;
            }

            return deserializedMessage;
        }

        public static string Serialize(SignedMessage sm)
        {
            XmlSerializer x = new XmlSerializer(sm.GetType());
            string serializedMessage = "";
            using (MemoryStream ms = new MemoryStream())
            {
                x.Serialize(ms, sm);
                serializedMessage = Encoding.ASCII.GetString(ms.ToArray());
            }

            return serializedMessage;
        }

        public static SignedMessage DeserializeSignedMessage(string signedMessage)
        {
            SignedMessage deserializedMessage = new SignedMessage();
            XmlSerializer x = new XmlSerializer(deserializedMessage.GetType());
            byte[] byteArray = Encoding.ASCII.GetBytes(signedMessage);
            using (MemoryStream stream = new MemoryStream(byteArray))
            {
                deserializedMessage = x.Deserialize(stream) as SignedMessage;
            }

            return deserializedMessage;
        }
    }
}
